<!DOCTYPE html>
<html>
<head>
  <title>map</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/ol.css" type="text/css">
  <script src="js/ol.js"></script>
  <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=c002f8d098d53ba8815a61fd11b67627"></script>
  <style>
      html, body, .map {
          margin: 0;
          padding: 0;
          overflow: hidden;
          height: 100%;
          width: 100%;
      }
      .map {
          position: absolute;
          top: 0;
          left: 0;
      }
      .swipe input[type=range] {
          margin: 0;
          padding: 0;
          width: 100%;
          position: absolute;
          top: calc(50% - 18px);
          left: 0;
          z-index: 99;
          outline: none;
          -webkit-appearance: none;
          height: 0;
          background-color: white;
      }

      .swipe input[type=range]::-webkit-slider-thumb {
          -webkit-appearance: none;
          height:36px;
          width: 36px;
          background-image: url();
          background-size: 100%;
          cursor: move;
      }

      .swipe .swipe-bar {
          position: fixed;
          width: 4px;
          height: 100%;
          content: ' ';
          top: 0;
          background: rgb(255, 0, 0);
          z-index: 9;
      }
  </style>
</head>
<body>
<div class="swipe">
  <input type="range" id="swipe"/>
  <div class="swipe-bar" id="swipeBar"></div>
</div>
<div id="olmap" class="map"></div>
<div id="amap" class="map" style="z-index: 9"></div>
<script>
  const center = [11760366.56, 4662347.84]
  const zoom = 4
  var olmap = new ol.Map({
    target: 'olmap',
    attributes: [],
    layers: [
      new ol.layer.Tile({
        source: new ol.source.XYZ({
          url: 'http://webrd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8'
        })
      })
    ],
    view: new ol.View({
      center: center,
      zoom: zoom
    })
  });

  var amap = new AMap.Map('amap', {
    zoom: zoom,
    center: ol.proj.toLonLat(center),
    features: ['bg','point','building'] // ['bg','point','road','building']
  });
  amap.on('mapmove', e => {
    const {lng, lat} = amap.getCenter()
    const zoom = amap.getZoom()
    const view = olmap.getView()
    view.setCenter(ol.proj.fromLonLat([lng, lat]))
    view.setZoom(zoom)
  })

  const map1 = document.getElementById('olmap')
  const map2 = document.getElementById('amap')
  const WIDTH = map1.clientWidth
  const HEIGHT = map1.clientHeight

  const swipe = document.getElementById('swipe')
  const MAX = 1000
  swipe.setAttribute('min', 0)
  swipe.setAttribute('max', MAX)
  swipe.setAttribute('value', MAX / 2)
  swipe.oninput = function(e) {
    setStyle()

  }
  setStyle()

  function setStyle() {
    const v = swipe.value
    const left = Math.ceil((v / MAX) * WIDTH)
    map1.style.clip = `rect(0, ${left}px, ${HEIGHT}px, 0)`
    map2.style.clip = `rect(0, ${WIDTH}px, ${HEIGHT}px, ${left}px)`
    // 设置bar样式
    const swipeBar = document.getElementById('swipeBar')
    swipeBar.style.left = `${left - 2}px`
    // 设置swipe样式
    const hWIDTH = MAX / 2
    const isLeft = v < hWIDTH
    const offsetL = Math.abs(((hWIDTH - v) / hWIDTH) * 18)
    swipe.style.left = isLeft ? `-${offsetL}px` : `${offsetL}px`
  }
</script>
</body>